package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Xml;

import com.tan.lbs.dao.GameRuleDAO;
import com.tan.lbs.entity.Condition;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.entity.Trigger;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.SQLiteDatabaseHelper;

public class GameRuleDAOImpl implements GameRuleDAO {
	
	private Context context;
	
	public GameRuleDAOImpl(Context context) {
		this.context = context;
	}

	@Override
	public List<String> getGameObjectAssociatedTriggerIDs(String gameID,String conditionType,String gameObjectID) {
		List<String> triggerIDs = new ArrayList<String>();
		String triggerID = null;
		String searchObjectID = null;
		String searchConditionType = null;
		SQLiteDatabase db = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/gamecondition.xml"), "UTF-8");
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if ("gamecondition".equals(parser.getName())) {
						searchObjectID = null;
						searchConditionType = null;
						triggerID = null;
					}
						if ("paramX".equals(parser.getName())) {
							searchObjectID  = parser.nextText();
						} else if ("triggerID".equals(parser.getName())) {
							triggerID = parser.nextText();
						} else if ("conditionType".equals(parser.getName())) {
							searchConditionType = parser.nextText();
						} 
					break;
				case XmlPullParser.END_TAG:
					if ("gamecondition".equals(parser.getName())) {
						String test = searchObjectID + searchConditionType;
						if(searchObjectID.equals(gameObjectID) && searchConditionType.equals(conditionType)) {
							SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
									context, Constants.LOCAL_DB_NAME);
							 db = dbHelper.getReadableDatabase();
							
							Cursor cursor = db.query("trigger", new String[] { 
									"status" }, "triggerID=?",
									new String[] { triggerID }, null, null,
									null);
							if(cursor.moveToNext()) {
								String status = cursor.getString(cursor.getColumnIndex("status"));
								if(status.equals(Trigger.NOT_TRIGGER_STATUS)) {
									triggerIDs.add(triggerID);
								}
							}
						}
						
					}
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(db!=null) {
				db.close();
			}
		}
		return triggerIDs;
	}
	
	public Trigger loadTrigger(String triggerID,String gameID) {
		Trigger trigger = new Trigger(triggerID);
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/gametrigger.xml"), "UTF-8");

			int event = parser.getEventType();
			String searchID;
			boolean found = false;
			while (event != XmlPullParser.END_DOCUMENT) {
				if (event == XmlPullParser.START_TAG) {
					if ("triggerID".equals(parser.getName())) {
						searchID = parser.nextText();
						if (searchID.equals(triggerID)) {
							found = true;
						}
					} else if ("textAfterTrigger".equals(parser.getName()) && found) {
						trigger.setTriggerText(parser.nextText());
					} else if ("actionType".equals(parser.getName()) && found) {
						trigger.setActionType(parser.nextText());
					} else if ("actionObjectID".equals(parser.getName()) && found) {
						trigger.setActionObjectID(parser.nextText());
					} 
				} else if (event == XmlPullParser.END_TAG && found && "gameTrigger".equals(parser.getName())) {
					break;
				}
				event = parser.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		//fill in the conditions
		List<Condition> conditions = trigger.getConditions();
		Condition condition = null;
		String searchTriggerID = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/gamecondition.xml"), "UTF-8");

			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if ("gamecondition".equals(parser.getName())) {
						searchTriggerID = null;
						condition = new Condition();
					}
					if (condition != null) {
						if ("conditionType".equals(parser.getName())) {
							condition.setConditionType(parser.nextText());
						} else if ("paramX".equals(parser.getName())) {
							condition.setParamX(parser.nextText());
						} else if ("paramY".equals(parser.getName())) {
							condition.setParamY(parser.nextText());
						} else if ("triggerID".equals(parser.getName())) {
							searchTriggerID = parser.nextText();
						} 
					}
					break;
				case XmlPullParser.END_TAG:
					if ("gamecondition".equals(parser.getName())) {
						if(searchTriggerID.equals(triggerID)) {
							conditions.add(condition);
						}
						condition = null;
					}
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return trigger;
	}
	
	public void updateTriggerStatus(String triggerID,String status) {
		SQLiteDatabaseHelper dbHelper = new SQLiteDatabaseHelper(
				context, Constants.LOCAL_DB_NAME);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("status", status);
		db.update("trigger", values, "triggerID=?", new String[]{triggerID});
		db.close();
	}
	
	@Override
	public List<String> retrieveTriggerIDs(String gameID) {
		List<String> triggerIDs = null;
		Trigger triggerID = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(
					context.getResources().getAssets()
							.open("gameFiles/" + gameID + "/gametrigger.xml"), "UTF-8");

			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_DOCUMENT:
					triggerIDs = new ArrayList<String>();
					break;
				case XmlPullParser.START_TAG:
						if ("triggerID".equals(parser.getName())) {
							triggerIDs.add(parser.nextText());
						} 
					
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return triggerIDs;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
